package com.polymathcoder.dpastry.core.network;

import com.polymathcoder.dpastry.dPastry;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.compression.ZlibDecoder;
import org.jboss.netty.handler.codec.compression.ZlibEncoder;
import org.jboss.netty.handler.codec.compression.ZlibWrapper;

import static org.jboss.netty.channel.Channels.pipeline;

/**
 * Created by IntelliJ IDEA.
 * User: Drew
 * Date: Jul 7, 2010
 * Time: 10:24:26 PM
 * To change this template use File | Settings | File Templates.
 */
public class TcpTransportPipelineFactory implements ChannelPipelineFactory {

    protected dPastry.ServiceHandleImpl<TcpTransportService> parent;

    public TcpTransportPipelineFactory(dPastry.ServiceHandleImpl<TcpTransportService> parent) {
        this.parent = parent;
    }

    @Override
    public ChannelPipeline getPipeline() throws Exception {
        ChannelPipeline pipeline = pipeline();

        //Stream compression, just because it's something to put here while I work out the details
        pipeline.addLast("deflater", new ZlibEncoder(ZlibWrapper.GZIP));
        pipeline.addLast("inflater", new ZlibDecoder(ZlibWrapper.GZIP));

        //Stream Encryption

        //Magic Number / Pastry Version Checking : MagicNumber<InetSocketAddress, ByteBuffer>: adds a special header to each Message, Socket

        //IpService : IpService<InetSocketAddress, ByteBuffer>: can be used to determine InetAddress of a new node, request information about the node

        //SourceRoute : SourceRoute<SourceRoute<MultiInetSocketAddress>, ByteBuffer>: supports a SourceRoute to a node

        //Message Destination Identity : LowerIdentity<SourceRoute<MultiInetSocketAddress>, ByteBuffer>: heads a message/socket with the intended destination, (a NodeHandle) so that if a node reboots, it will reject messages to the previous instance.

        //Adaptive Polling / Liveness Check : Liveness<SourceRoute<MultiInetSocketAddress>, ByteBuffer>: uses an adaptive polling to determine if a Route is alive, suspected or dead, also exports proximity and liveness

        //SourceRouteManager : SourceRouteManager<MultiInetSocketAddress, ByteBuffer>: Aggregates the info from the !Liveness/SourceRoute layers for all possible routes to a destination to determine !Liveness/!Proximity and the best route. Uses the LeafSet to select routes.

        // !Priority<MultiInetSocketAddress, ByteBuffer>: Holds a queue of outgoing messages and sends the messages over a Socket (also has to manage a socket to send the messages)

        // UpperIdentity<NodeHandle, ByteBuffer>: converts NodeHandle to MultiInetSocketAddress, and keeps track of who is already considered faulty (DEAD_FOREVER) at that address

        // Encode/Decode Message!
        // !CommonAPI<NodeHandle, RawMessage>: serializes/deserializes the message

        pipeline.addLast("handler", new TcpTransportHandler(this.parent));

        return pipeline;
    }
}
